package content;

import content.views.About;
import content.views.BaseView;
import content.views.Contact;
import content.views.Home;
import content.views.Links;
import content.views.Works;
import feffects.easing.Expo;
import feffects.easing.Quad;
import feffects.easing.Quint;
import feffects.Tween;
import haxe.Timer;
import js.Lib;
import net.address.Address;
import params.GlobalParams;
import ui.Background;
import ui.nav.Nav;
import js.JQuery;

/**
 * Content Manager
 * @author PRASS
 */
 
class ContentManager 
{
	static public var isMoving : Bool;
	
	//private var _this 			: JQuery;
	
	//private var _proxyNav 		: JQuery;
	
	private var _vHome			: Home;
	private var _vWorks			: Works;
	private var _vAbout			: About;
	private var _vContact		: Contact;
	private var _vLinks			: Links;

	private var _currentView	: BaseView;
	private var _oldView		: BaseView;
	
	public function new() 
	{
		//_this = new JQuery("#content");
		_currentStep = 0;
		
		_vHome 		= new Home("home", "tpl/home.html");
		_vWorks 	= new Works("works");
		_vAbout 	= new About("about", "tpl/about.html");
		_vContact 	= new Contact("contact", "tpl/contact.html");
		_vLinks 	= new Links("links", "tpl/links.html");
		
		_currentStep 	= -1;
		_firstLoad		= true;
		
		_oldView = _currentView = _vHome;
		
		Address.init( this, _loadPage );
	}
	
	// Referenced by Address
	private function _loadPage( hash : String ) : Void
	{
		if(_currentView != null) _currentView.hide();
		
		isMoving = true;
		
		var step : Int = 0;
		
		switch( hash )
		{
			case "":
			case GlobalParams.ID_MAIN:
				_currentView = _vHome;
			case GlobalParams.ID_WORKS:
				step = 1; 
				_currentView = _vWorks;
			case GlobalParams.ID_ABOUT:
				step = 2;
				_currentView = _vAbout;
			case GlobalParams.ID_CONTACT:
				step = 3;
				_currentView = _vContact;
			case GlobalParams.ID_LINKS:
				step = 4;
				_currentView = _vLinks;
		}
		
		//check
		if (step == _currentStep) return;
		
		_currentView.setAlpha(0);
		_currentView.show();
		_currentView.resize(_currentWidth, _currentHeight);
		
		if ( step > _currentStep )
		{
			_currentView.setHeight(0);
			_currentView.tween(0, _currentView.setHeight, _currentHeight, 750, Quad.easeIn, _end);
			
			_currentView.setAlpha(1);
			_oldView.dom.css("z-index", "1");
			_currentView.dom.css("z-index", "2");
			
		} else {
			
			/*if ( _oldView.rootID == "contact")
			{
				//Timer.delay( _end, 1499);
				//Lib.alert("!");
				untyped { jQuery("#contact").slideUp(1500, "easeOutExpo"); }
				
			} else {*/
			_oldView.tween(_currentHeight, _oldView.setHeight, 0, 1500, Expo.easeOut, _end);
			//Timer.delay( _end, 1499);
			
			_currentView.setAlpha(1);
			//_oldView.dom.slideUp( 1000, null, null );
			//Lib.alert( _oldView.rootID);
			//untyped { _oldView.slideUp(1500, 'easeOutExpo'); }
			//}
			
		}
		
		_currentStep = step;
	}

	
	private function _end (  ) : Void
	{
		if (_firstLoad == false) 
			_oldView.removeChild();
		else
			_firstLoad = false;
			
		_oldView = _currentView;
		_currentView.play();
		
		isMoving = false;
	}
	
	public function resize( _w : Int, _h : Int ) : Void
	{
		_currentWidth = _w;
		_currentHeight = _h;
		
		if ( _currentView == null ) return;

		_currentView.resize(_currentWidth, _currentHeight);
	}
	
	private var _currentStep	: Int;
	private var _currentWidth	: Int;
	private var _currentHeight	: Int;
	
	private var _firstLoad		: Bool;
}